package lib.standard.f;

import android.app.Activity;
import android.content.Intent;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.Utils;
import com.idlefish.flutterboost.FlutterBoost;
import com.idlefish.flutterboost.FlutterBoostDelegate;
import com.idlefish.flutterboost.containers.FlutterBoostActivity;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.HashMap;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.annotation.StringDef;
import io.flutter.embedding.android.FlutterActivityLaunchConfigs;
import io.flutter.embedding.engine.FlutterEngine;

/**
 * @author Lynch
 * @date 2021/4/9 12:28
 * @desc 调用flutter端 与 flutter调的native的实现(业务层的交互)
 */
public class Helper {
    /**
     * Flutter页面
     *
     * @param router 约定的具体页面/模块入口
     */
    public static void startUI(String router) {
//        FlutterBoost.instance().open(router,params);

//        FlutterBoost.instance().close(router);
    }

    /**
     * @param classes 键为常量 featureNameSubname
     */
    public static void init(@NonNull Map<String, Class<Activity>> classes) {
//        ConfigsForHttpretrofitSDK.setToken("testtoken");
        FlutterBoost.instance().setup(Utils.getApp(), new FlutterBoostDelegate() {

            @Override
            public void pushNativeRoute(String pageName, Map<String, Object> arguments) {
                LogUtils.e("pushNativeRoute:" + pageName + "\n" + arguments);
                Intent intent = new Intent(FlutterBoost.instance().currentActivity(), classes.get(pageName));
                FlutterBoost.instance().currentActivity().startActivity(intent);
            }

            @Override
            public void pushFlutterRoute(String pageName, String uniqueId, Map<String, Object> arguments) {
                LogUtils.e("pushFlutterRoute:" + pageName + "\n" + arguments + "\nuniqueId:" + uniqueId);
                Intent intent = new FlutterBoostActivity.CachedEngineIntentBuilder(FlutterBoostActivity.class, FlutterBoost.ENGINE_ID)
                        .backgroundMode(FlutterActivityLaunchConfigs.BackgroundMode.opaque)
                        .destroyEngineWithActivity(false)
                        .url(pageName)
                        .urlParams(arguments)
                        .build(FlutterBoost.instance().currentActivity());
                FlutterBoost.instance().currentActivity().startActivity(intent);
            }

        }, FlutterEngine::getPlugins);
    }


    public static void close(@Route String fRoute) {
        FlutterBoost.instance().close(fRoute);
    }

    public static void open(@Route String fRoute, @NonNull Map<String, Object> args) {
        FlutterBoost.instance().open(fRoute, args);
    }

    public static void open(@Route String fRoute) {
        FlutterBoost.instance().open(fRoute, new HashMap<>());
    }

    @StringDef({Route.featureSome, Route.login})
    @Retention(RetentionPolicy.SOURCE)
    public @interface Route {
        String featureSome = "/featureSome";
        String login = "/login";
    }
}
